package com.tang.crm.controller;

import com.tang.crm.base.BaseController;
import com.tang.crm.base.ResultInfo;
import com.tang.crm.dto.UserDto;
import com.tang.crm.entity.User;
import com.tang.crm.vo.UserVo;
import com.tang.crm.service.UserService;
import com.tang.crm.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 用户管理
 * @author SHIRELY阳
 */
@Controller
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 进入用户列表页面
     * @return
     */
    @RequestMapping("/user/index")
    public String index(){
        return "user/user";
    }

    /**
     * 进入用户信息修改界面
     * @param request
     * @return
     */
    @RequestMapping("/user/toSettingPage")
    public String toSettingPage(HttpServletRequest request) {
        int userId = LoginUserUtil.releaseUserIdFromCookie(request);
        request.setAttribute("user", userService.getById(userId));
        return "setting";
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    @ResponseBody
    public ResultInfo login(User user){
        ResultInfo resultInfo = new ResultInfo();
        UserVo userVo = userService.login(user);
        resultInfo.setResult(userVo);
        return resultInfo;
    }

    @PostMapping("/user/updatePwd")
    @ResponseBody
    public ResultInfo updateUserPassword(HttpServletRequest request, String oldPassword,
                                         String newPassword, String confirmPassword){
        ResultInfo resultInfo = new ResultInfo();
        int id = LoginUserUtil.releaseUserIdFromCookie(request);
        userService.updateUserPassword(id, oldPassword, newPassword, confirmPassword);
        return resultInfo;
    }

    /**
     * 进入修改用户密码界面
     * @return
     */
    @RequestMapping("/user/toPasswordPage")
    public String toUpdatePwd(){
        return "user/password";
    }

    /**
     * 查询所有销量
     * @return
     */
    @RequestMapping("/user/queryAllSales")
    @ResponseBody
    public List<Map<String, Object>> queryAllSales(){
        return userService.queryAllSales();
    }


    /**
     * 多条件查询用户数据
     * @param userDto
     * @return
     */
    @RequestMapping("/user/list")
    @ResponseBody
    public Map<String, Object> selectByParams(UserDto userDto){
        return userService.selectByParams(userDto);
    }

    /**
     * 添加用户数据
     * @param user
     * @return
     */
    @RequestMapping("/user/add")
    @ResponseBody
    public ResultInfo saveUser(User user){
        userService.saveUser(user);
        return success("用户添加成功");
    }

    /**
     * 更新用户数据
     * @param user
     * @return
     */
    @RequestMapping("/user/update")
    @ResponseBody
    public ResultInfo updateUser(User user){
        userService.updateUser(user);
        return success("用户数据更新成功");
    }

    @RequestMapping("/user/toAddOrUpdateUserPage")
    public String toAddOrUpdateUserPage(Integer id, HttpServletRequest request){
        // 判断id是否为空
        if (id != null){
            User user = userService.getById(id);
            request.setAttribute("userInfo", user);
        }
        return "user/add_update";
    }

    @PostMapping("/user/delete")
    @ResponseBody
    public ResultInfo deleteUserList(@RequestParam("ids") List<Integer> idList){
        userService.deleteUser(idList);
        return success("删除用户成功");
    }

    /**
     * 查询所有的客户经理
     * @return
     */
    @RequestMapping("/user/queryAllCustomerManagers")
    @ResponseBody
    public List<Map<String, Object>> selectAllManagers() {
        return userService.selectAllManagers();
    }
}
